﻿(async () => {
    await window.ris.initialize();
    $(() => {
        const oid = getUrlSearch('oid');
        const $eventEmitter = $('body > .container');
        let form = $('.exam-form').amisForm(); // 此处更改
        const toolbar = $('.toolbar').dxToolbar({}).dxToolbar('instance');
        const tools = $.getRegisterTools($eventEmitter, null, null, null, form, toolbar);
        $.get('/api/get-print-options').then(result => {
            let printOptions = [];
            if (result.success) {
                printOptions = result.data;
            }
            //打印
            if (printOptions instanceof Array && printOptions.length > 0) {
                printOptions = printOptions.filter(option => option.id !== 'TempReport');
                printOptions.forEach(e => {
                    e.icon = 'print';
                    e.onClick = (e) => {
                        $eventEmitter.trigger('print', {
                            quickFilter:null,
                            advancedSearch:null,
                            examGrid: null,
                            data: e.itemData,
                            form:form
                        });
                    };
                });
                tools.print.options.items = printOptions;
            }
            tools.waitingQueue.options.items = [tools.suspend.options, tools.cancelSuspend.options, tools.top.options, tools.previous.options, tools.next.options, tools.bottom.options];
            let toolbarOptions = [tools.save, tools.submit, tools.revoke, tools.edit, tools.print, tools.viewReport, tools.viewImage, tools.arrival, tools.waitingQueue];
            toolbar.option('items', toolbarOptions);
        });

        $('body > .container > .main').dxScrollView({
            scrollByContent: true,
            scrollByThumb: true,
        });

        ris.showLoading();

        // 公共请求
        Promise.all([$.get(`/api/register/${oid}/get-view`), $.get(`/api/register/${oid}/get-exam`)]).then(result => {
            ris.hideLoading();
            let view = result[0].view;
            if (view && typeof view === 'string') {
                view = JSON.parse(view);
            }
            if (result[1].success) {
                let formData = result[1].data;
                document.title = `${formData.patient?.name ?? formData.accessionNumber ?? formData.patient?.patientId} | 登记`;
                //如果已经提交过，则不能保存再提交，只能撤回继续编辑
                //examStatus
                tools.updateToolbarItems(formData);
                const context = {
                    isDisabled: formData.examStatus !== 0,
                    url: '/'
                };
                if (formData.examMethods && formData.examMethods instanceof Array) {
                    formData.examMethods = formData.examMethods.map(e => e.oid).join(',');
                }
                if (formData.patientFeatures && formData.patientFeatures instanceof Array) {
                    formData.patientFeatures = formData.patientFeatures.map(e => e.oid).join(',');
                }
                formData.namePinYin = formData.patient.namePinYin;
                formData.birthDate = formData.patient.birthDate;
                form.updateView(view, formData, context);
                registerSelectAppointmentPlan(form);  // 指定自定义组件注册
            } else {
                DevExpress.ui.notify(result.error || '接口请求失败', 'error');
            }
        }).catch(() => {
            ris.hideLoading();
        });
    });
})();